/*
 * display_hal.c
 *
 *  Created on: Sep 13, 2013
 *      Author: Ken Arok
 */


#include "config_board.h"

#if BOARD_2_1_USED

#include "mydefs.h"
#include "st7066u.h"
#include "gpio.h"
#include "delay.h"
#include "display_hal.h"

/* LCD module. */
#define LMB204BFC_LCDMODULE_USED		1

#if LMB204BFC_LCDMODULE_USED

void display_init(void)
{
	st7066u_init();
}


void display_set_pos(uint8_t _row, uint8_t _col)
{
	if((_col > 0) && (_col < 21)) {
		switch(_row)
		{
			case 1:
				st7066u_send_instruction((const uint8_t)(0x80 + _col - 1));
			break;

			case 2:
				st7066u_send_instruction((const uint8_t)(0xC0 + _col - 1));
			break;

			case 3:
				st7066u_send_instruction((const uint8_t)(0x94 + _col - 1));
			break;

			case 4:
				st7066u_send_instruction((const uint8_t)(0xD4 + _col - 1));
			break;

			default: break;
		}
	}

}


void display_put(uint8_t _row, uint8_t _col, const char _char)
{
	if(_col > 20) {
		_col = 1;
		_row++;
	}

	if((_row > 0) && (_row < 5)) {
		if(_col) {
			display_set_pos(_row, _col);
			st7066u_send_data((const uint8_t)_char);
		}
	}
}


void display_print(uint8_t _row, uint8_t _col, const char *_string)
{
	const uint8_t *_ptr = (const uint8_t *)_string;

	if(_col > 20) {
		_col = 1;
		_row++;
	}

	if((_row < 5) && (_col)) {
		display_set_pos(_row, _col);
		while(*_ptr && (_row < 5)) {
			st7066u_send_data(*_ptr);
			_ptr++;
			_col++;

			if(_col > 20) {
				_col = 1;
				_row++;
			}
		}
	}
}


void display_clear(void)
{
	const uint8_t _instr = 0x01;

	st7066u_send_instruction(_instr);
	delay_ms(5);
}


void display_control(const uint8_t _ctrl)
{
	st7066u_send_instruction(_ctrl);
	delay_ms(5);
}


void display_turn_backlight(uint8_t _on_off)
{
	switch(_on_off) {
		case iON:
			gpio_set_pin_low(LCD_BLON_GPIO);
		break;

		case iOFF:
			gpio_set_pin_high(LCD_BLON_GPIO);
		break;

		default: break;
	}
}

#else

#endif /* LMB204BFC_LCDMODULE_USED. */

#endif /* BOARD_2_1_USED */
